<?php

namespace app\common\controller;

use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Session;


class Syytem extends Controller
{
	
	 //发送手机验证码
    public function CodePost($mobile)
    {
        if ($this->checkMobile($mobile)) {
            $mobilecode = db('mobilecode');
            //生成编码
            $code = rand(100000, 999999);
            $data['code'] = $code;
            //var_dump($code);die;
            $data['mobile'] = $mobile;
            $data['uptime'] = time();
            $songtime = time() - 60;
            $content = "【仙鹤堂】亲！您的会员注册验证码是{$code}，请在10分钟内输入！";
            $data['content'] = "亲！您的会员注册验证码是{$code}，请在10分钟内输入！";
            $xx = $mobilecode->where('mobile=' . $mobile . ' AND uptime>' . $songtime)->count();
            if ($xx > 0) {
                //$z="60秒内只能发送一次验证码！";
                return 4003;
                exit;
            }
            //写入数据库
            $mobilecode->insert($data);
            //短信设置
            $x = "手机号码错误！";
            $y = "验证码发送失败！";

            if ($this->SmsPost($mobile, $content) == 1) {

                return $code;
            } else {
                return 4002;
            }
        } else {
            return 4001;
        }
    }



	
//验证手机号
public function checkMobile($mobile)
{
    if (preg_match("/^((\+?86)|(\(\+86\)))?(13[0123456789][0-9]{8}|15[0123456789][0-9]{8}|16[0123456789][0-9]{8}|18[0123456789][0-9]{8}|19[0123456789][0-9]{8}|17[0123456789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/", $mobile)) {
        if (is_numeric($mobile)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}
	
	
	
//发送短信接口配置
    public function SmsPost($mobile, $content)
    {
        $url = 'http://jiekou.56dxw.com/sms/HttpInterface.aspx';
        $params = array('comid' => '3802', 'smsnumber' => '10690');
        $params['username'] = 'eacion';
        $params['userpwd'] = 'eac0008';
        $params['sendtime'] = '';
        $params['handtel'] = $mobile;
        $params['sendcontent'] = iconv('UTF-8', 'GB2312//IGNORE', $content);
        return $this->Sms_http($url, $params, 'GET');
    }
	
	
	
	
	   /*发送短信息函数*/
    public function Sms_http($url, $params, $method = 'POST')
    {

       // $cookie_jar = tempnam('./tmp', 'cookie');
        $ci = curl_init();
        $params = http_build_query($params);
        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, TRUE);
                if (!empty($params)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
                    $this->postdata = $params;
                }
                break;
            case 'PUT' :
                curl_setopt($ci, CURLOPT_PUT, true);
                if (!empty($params)) {
                    $url = strpos('?', $url) === false ? "{$url}?{$params}" : "{$url}&{$params}";
                }
                break;
            case 'DELETE':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($params)) {
                    $url = strpos('?', $url) === false ? "{$url}?{$params}" : "{$url}&{$params}";
                }
                break;
            case 'GET':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'GET');
                if (!empty($params)) {
                    $url = strpos('?', $url) === false ? "{$url}?{$params}" : "{$url}&{$params}";
                }
        }

        curl_setopt($ci, CURLOPT_URL, $url);    //设置你需要抓取的URL
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($ci);
        curl_close($ci);
        return $res;
    }

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
